package cao.oschina.widget.dialog;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import cao.oschina.R;


/**
 * 绑定界面的Dialog
 * <p/>
 * Created by haidy on 14/12/23.
 */
public class LoginDialog extends AlertDialog {
    /**
     * Create a Dialog window that uses the default dialog frame style.
     *
     * @param context The Context the Dialog is to run it.  In particular, it
     *                uses the window manager and theme in this context to
     *                present its UI.
     */
    public LoginDialog(Context context) {
        this(context, R.style.dialog_style);
    }

    /**
     * Create a Dialog window that uses a custom dialog style.
     *
     * @param context The Context in which the Dialog should run. In particular, it
     *                uses the window manager and theme from this context to
     *                present its UI.
     * @param theme   A style resource describing the theme to use for the
     *                window. See <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes">Style
     */
    public LoginDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void init() {
        super.init();
        setBodyView(R.layout.dialog_binding_view);
        setTitleView(R.layout.dialog_load_title_view);
        setCanceledOnTouchOutside(false);
        findViewById(R.id.email).requestFocus();
    }

    /**
     * 开始加载
     */
    public void startLoading() {
        findViewById(R.id.pb).setVisibility(View.VISIBLE);
        findViewById(R.id.pwd).setEnabled(false);
        findViewById(R.id.email).setEnabled(false);
        hideSoftKeyboard();
    }

    /**
     * 结束加载
     */
    public void stopLoading() {
        findViewById(R.id.pb).setVisibility(View.GONE);
        findViewById(R.id.pwd).setEnabled(true);
        findViewById(R.id.email).setEnabled(true);
    }

    /**
     * 获取密码
     *
     * @return 密码
     */
    public String getPwd() {
        return ((EditText) findViewById(R.id.pwd)).getText().toString();
    }

    /**
     * 获取用户名
     *
     * @return sid
     */
    public String getEmail() {
        return ((EditText) findViewById(R.id.email)).getText().toString();
    }

    public void hideSoftKeyboard() {
        try {
            ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getBodyView().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception ignored) {
        }
    }

}
